# -*- encoding : utf-8 -*-
class Book < ActiveRecord::Base
  
  ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
  errors = Array(instance.error_message).join(',')
  %(#{html_tag}<span class="validation-error">&nbsp;#{errors}</span>).html_safe
end
   validates :title, :author, :publisher, :image_url, :description, :presence => true
   validates :price, :quantity, :free_quantity, :numericality => { :only_integer => true }
  def self.search(search)
    if search
      find(:all, :conditions => ['title LIKE ?', "%#{search}%"])
    else
      find(:all)
    end
  end
  
  def self.getBookTitle(id)
    book = Book.find(id)
    return book.title
  end
  
  belongs_to :book_category
  has_many :book_borrows
  has_many :book_reserves
  
end